package com.zz.springboot.controller;

import com.zz.springboot.dto.UserDto;
import com.zz.springboot.exception.BizException;
import com.zz.springboot.req.vo.LoginRequest;
import com.zz.springboot.req.vo.UserVO;
import com.zz.springboot.req.vo.UserValidDaysVo;
import com.zz.springboot.resp.RespTemplate;
import com.zz.springboot.resp.enums.RespCodeEnum;
import com.zz.springboot.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class UserController {

    @Autowired
    UserService userService;

    /**
     * 注册
     *
     * @param userVO
     * @return
     */
    @RequestMapping("/register")
    public RespTemplate register(UserVO userVO) {
        // 1. 判断用户是否存在
        boolean flag = userService.verifyOnlyUsername(userVO.getUsername());
        if (!flag) {
            log.error("用户[{}]已存在", userVO.getUsername());
            throw new BizException(RespCodeEnum.USER_ALREADY_EXISTS);
        }
        // 2. 注册用户
        log.info("用户[{}]开始注册", userVO.getUsername());
        userService.register(userVO.getUsername(), userVO.getPassword());
        log.info("用户[{}]注册成功", userVO.getUsername());
        return RespTemplate.success();
    }

    /**
     * 登录
     *
     * @param loginRequest
     * @return
     */
    @RequestMapping("/login")
    public RespTemplate login(LoginRequest loginRequest) {
        String username = loginRequest.getUsername();
        String password = loginRequest.getPassword();

        // 获取 Shiro Subject
        Subject subject = SecurityUtils.getSubject();
        // 构建 Token
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        // 执行登录（会触发 Realm 的 doGetAuthenticationInfo）
        subject.login(token);
        log.info("用户[{}] 登录成功", username);
        //设置session
        Session session = subject.getSession();
        session.setAttribute("username", username);

        return RespTemplate.success();

    }

    /**
     * 登出功能
     *
     * @return
     */
    @RequestMapping("/logout")
    public RespTemplate logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return RespTemplate.success();
    }

    @RequiresRoles("admin")
    @RequestMapping("/setValidDays")
    public RespTemplate setValidDays(UserValidDaysVo userValidDaysVo){
        String username = (String) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
        log.info("操作人为{}",username);
        userService.setUserValidDays(userValidDaysVo.getUsername(), userValidDaysVo.getValidDays());
        return RespTemplate.success();
    }
}
